
//index.js
//获取应用实例
const app = getApp();
// const user = wx.getStorageSync("user");
var user_token = wx.getStorageSync('user_token');
const base_url = app.globalData.base_url;
const util = require('../../utils/util.js');
var path = '';
var urlWithArgs = '';
Page({
  data: {
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onLoad: function (options) {
    
    path = decodeURIComponent(options.re);
    // console.log('123')
    // console.log(path)
    // console.log(options);
    // urlWithArgs = path + '?'
    // for (var key in options) {
    //   if(key=='id'){
    //     var value = options[key]
    //     urlWithArgs += key + '=' + value + '&'
    //   }
    // }
    // urlWithArgs = urlWithArgs.substring(0, urlWithArgs.length - 1)

    // 查看是否授权
    wx.getSetting({
      success: function (res) {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称
          wx.getUserInfo({
            success: function (res) {
              console.log(res.userInfo)
            }
          })
        }
      }
    })
  },
  bindGetUserInfo: function (e) {
    if (e.detail.userInfo) {
      wx.login({
        success: function (res) {
          if (res.code) {
            wx.getUserInfo({
              withCredentials: true,
              success: function (res_user) {
                wx.request({
                  url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/getOpenId',
                  data: {
                    code: res.code
                  },
                  method: 'POST',
                  header: {
                    'content-type': 'application/x-www-form-urlencoded'
                  },
                  success: function (data) {
                    if (data.data.success == 1) {
                      wx.request({
                        //后台接口地址
                        url: 'https://lc.feiyouce.com/index.php?s=/api/Pub/login',
                        data: {
                          wx_openid: data.data.data.openid,
                          wx_headimgurl: res_user.userInfo.avatarUrl,
                          wx_nickname: res_user.userInfo.nickName
                        },
                        method: 'POST',
                        header: {
                          'content-type': 'application/x-www-form-urlencoded'
                        },
                        success: function (res) {
                          // console.log(app)
                          if (res.data.success == 1) {
                            app.globalData.userInfo = res.data.data;
                            // wx.setStorageSync('openId', res.data.openId);
                            let user = {
                              "openId": res.data.data.openId,
                              "user_token": res.data.data.user_token
                            }
                            wx.setStorageSync("user", JSON.stringify(user));
                            if (path == '' || path == 'undefined'){
                              urlWithArgs = '/pages/index/index'
                            }else{
                              urlWithArgs = '/pages/toPage/index?q=' + encodeURIComponent(path)
                            }
                            // console.log(urlWithArgs);
                            wx.redirectTo({
                              url: urlWithArgs
                            })
                          } else {
                            wx.showToast({
                              title: res.data.msg,
                              icon: 'none',
                              duration: 2000
                            })
                          }

                        }
                      })
                    } else {
                      wx.showToast({
                        title: res.data.msg,
                        icon: 'none',
                        duration: 2000
                      })
                    }

                  }
                })
              }, fail: function () {

              }, complete: function (res) {


              }
            })
          }
        }
      })
    } else {//不授权
      // wx.navigateBack({
      //   delta: 1
      // })
    }
  }
})